package com.allwees.core.common.util;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author mouhn
 * @since 2019-09-20
 */
public class SetUtil {

    public static void add(Set<Integer> set, Integer... params) {
        if(set == null || ArrayUtils.isEmpty(params)){
            return;
        }
        Arrays.stream(params).filter(Objects::nonNull).forEach(set::add);
    }

    public static void add(Set<Long> set, Long... params) {
        if(set == null || ArrayUtils.isEmpty(params)){
            return;
        }
        Arrays.stream(params).filter(Objects::nonNull).forEach(set::add);
    }

    public static void add(Set<String> set, String... strs) {
        if(set == null || ArrayUtils.isEmpty(strs)){
            return;
        }
        Arrays.stream(strs).filter(StringUtils::isNotBlank).forEach(set::add);
    }

    public static String[] toStringArray(Collection<String> coll) {
        if(CollectionUtils.isEmpty(coll)){
            return null;
        }
        return coll.toArray(new String[0]);
    }

    public static Integer[] toIntegerArray(Collection<Integer> coll) {
        if(CollectionUtils.isEmpty(coll)){
            return null;
        }
        return coll.toArray(new Integer[0]);
    }

    public static Long[] toLongArray(Collection<Long> coll) {
        if(CollectionUtils.isEmpty(coll)){
            return null;
        }
        return coll.toArray(new Long[0]);
    }

    public static Set<Integer> toInteger(String[] array) {
        if(ArrayUtils.isEmpty(array)){
            return new HashSet<>(0);
        }
        return Arrays.stream(array).map(Integer::new).collect(Collectors.toSet());
    }

    public static void main(String[] args) {
        String[] strs = {"111","112","113"};
        System.out.println(toInteger(strs));;
    }
}